package com.cn.web;

import com.cn.entity.KrpanoScene;
import com.cn.service.KrpanoSceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author: chenyuncong
 * @Date: 2020/5/14 22:04
 */
@RestController
@RequestMapping("scene")
public class KrpanoSceneWeb {

    @Value("${FILESERVERURL}")
    private String FILESERVERURL;

    @Value("${FILEPATH}")
    private String FILEPATH;

    @Autowired
    private KrpanoSceneService krpanoSceneService;

//    @RequestMapping("updateKrpanoSceneAllById")
    public String updateKrpanoSceneAllById(Long id) throws Exception {
        krpanoSceneService.updateKrpanoSceneAllById(id,350000L);
//        select MAX(h.scene_id) from krpano_sha_pan_layer h  limit 1;
//        select MAX(h.scene_id) from krpano_scene_relation h  limit 1;
//        select MAX(h.krpano_scene_id) from krpano_hotspot_relation h  limit 1;
        return null;
    }

    HashMap<String,String> map =new HashMap<String,String>(){{
        this.put("20161101","20161101");
        this.put("20161013","20161013");
        this.put("20161025","20161025");
        this.put("20161026","20161026");
        this.put("20161027","20161027");
        this.put("20161028","20161028");
        this.put("20161031","20161031");
        this.put("20161103","20161103");
//        this.put("20161101","20161101");
    }};

//    @GetMapping("saveKrpanoScene")
    public void saveKrpanoScene() throws Exception {
        String str = "";
        System.out.println(FILESERVERURL);
        System.out.println(FILEPATH);
//        KrpanoScene krpanoScene = new KrpanoScene();
//        krpanoSceneService.save(krpanoScene);
        checkFile(FILESERVERURL,FILEPATH);
    }


    public  List<String> checkFile(String FILESERVERURL, String FILEPATH)  {
        try{
            KrpanoScene krpanoScene =null;
            List<KrpanoScene> ksList = new ArrayList<>();
            String rootPath=null;

            if(FILEPATH.indexOf("ihkvr")<0){
                rootPath=FILEPATH+"/sdd/vr/krpano/panorama/";
            }
            if(FILEPATH.indexOf("vr_upload")>=0){
                rootPath=FILEPATH+"/vr/krpano/panorama/";
            }
            System.out.println("rootPath:"+rootPath);
            File directory = new File(rootPath);
            if (directory.isDirectory()) { //扫描如果是文件夹就进入
                File[] filelist = directory.listFiles(); //列出文件夹里面的内容
                File toFile = null;
                for (int i = 0; i < filelist.length; i++) {  //循环
                    if (filelist[i].isDirectory()) {   //是否是文件夹 文件夹是20200514 样子
                        toFile = new File(filelist[i].getAbsolutePath());
                        System.out.println(filelist[i].getAbsolutePath());
                        String thpscheck=filelist[i].getAbsolutePath();
//                        thpscheck=thpscheck.substring(thpscheck.length()-8,thpscheck.length());
//                        System.out.println(thpscheck);
//                        if (map.containsKey())
                        if(checkfile(thpscheck)){
                            continue;
                        }
                        File[] tofilelist = toFile.listFiles();
                        for (int n = 0; n < tofilelist.length; n++) {
//                        System.out.println(tofilelist[n].getAbsolutePath());
                            if (tofilelist[n].isDirectory()) {
                                String path = tofilelist[n].getAbsolutePath(); //拿到了地址
                                path=path.replace(FILEPATH,FILESERVERURL); //转成
                                int ind = path.indexOf("_tiles");
                                Long addTime=Long.parseLong(path.substring(ind-13,ind));
                                krpanoScene=new KrpanoScene();
                                krpanoScene.setCreationDate(new Date(addTime));
                                krpanoScene.setModDate(new Date(addTime));
                                krpanoScene.setMaxUrl(path);
                                krpanoScene.setMinUrl(path+"/thumb.jpg");
                                krpanoScene.setCutSceneStatus(2);
                                krpanoScene.setCutSucceed(true);
                                ksList.add(krpanoScene);
//                            System.out.println(path);
                            }
                        }
                        if(!ksList.isEmpty()){
                            krpanoSceneService.saveBatch(ksList);
                            ksList.clear();
                        }
                    }

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    boolean checkfile(String path){
        for(String k : map.keySet()){
            if(path.indexOf(k)>=0){
                return true;
            }
        }
        return false;
    }

}
